package com.siss.tenancy.util;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.*;

public class PdfUtils {

    //创建带有居中title的pdf对象
    public static PdfDocument createPdfObjectByTitleCenter(String title,int fontSize,String fontName,Color titleClour)throws Exception{
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4);
        page = doc.getPages().add(PdfPageSize.A4);
        for (int i = 0; i < 11; i++) {
            page = doc.getPages().add(PdfPageSize.A4);
        }
        Double height = page.getCanvas().getSize().getHeight();
        Double wedith = page.getCanvas().getSize().getWidth();
        System.out.println(height+"---"+wedith);
        PdfStringFormat format1 = new PdfStringFormat();
        format1.setAlignment(PdfTextAlignment.Center);//设置文字居中
        if(StringUtil.isEmpty(fontName)){
            fontName = "黑体";
        }
        if(fontSize<=0){
            fontSize = 14;
        }
        PdfTrueTypeFont font1= new PdfTrueTypeFont(new Font(fontName, Font.PLAIN,fontSize),true);
        if(null == titleClour){
            titleClour = Color.BLACK;
        }
        PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(titleClour));
        page.getCanvas().drawString(title, font1, brush1, new Point2D.Double(page.getActualBounds(true).getWidth() / 2, 0),format1);
        double contentHeigth = height-fontSize;
        System.out.println(contentHeigth);
        if(true){
            String body = readFileToString("C:\\Users\\admin\\Desktop\\bodyText.txt");

            //创建PdfStringFormat对象
            PdfStringFormat format2 = new PdfStringFormat();
            format2.setParagraphIndent(20);//设置段首缩进

            //创建Rectangle2D对象
            Rectangle2D.Double rect = new Rectangle2D.Double(0, 30, page.getActualBounds(true).getWidth(),page.getActualBounds(true).getHeight());
            PdfTrueTypeFont font2= new PdfTrueTypeFont(new Font("黑体",Font.PLAIN,10),true);
            PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
            //使用drawString方法在矩形区域绘制主体文字
            page.getCanvas().drawString(body, font2, brush2, rect,format2);
        }

        doc.saveToFile("C:\\Users\\admin\\Desktop\\test.pdf");
        return doc;
    }

    public static void main(String[] args) throws Exception{
        createPdfObjectByTitleCenter("深圳市世鹏资产评估房地产土地估价顾问有限公司走访计划和检查记录",0,"",Color.PINK);
    }

    private static String readFileToString(String filepath) throws FileNotFoundException, IOException {

        StringBuilder sb = new StringBuilder();
        String s ="";
//        BufferedReader br = new BufferedReader(new FileReader(filepath));
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"GBK"));
        while( (s = br.readLine()) != null) {
            sb.append(s + "\n");
        }
        br.close();
        String str = sb.toString();
        return str;
    }
}
